package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 智能设备Controller
 *
 * @author alexis
 * @date 2025-06-01
 */
@Api(tags = "智能设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询智能设备列表
     */
    @ApiOperation("查询智能设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("智能设备查询条件") Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     *
     * @return
     */
    @ApiOperation("同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @ApiOperation("查询所有产品列表")
    @GetMapping("/allProduct")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 注册设备
     *
     * @param deviceDto
     * @return
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success();
    }

    /**
     * 获取设备详细信息
     *
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    public AjaxResult getInfo(@PathVariable String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.getInfo(iotId);

        return success(deviceDetailVo);
    }

    /**
     * 查看设备上报的数据
     *
     * @param iotId
     * @return
     */
    @ApiOperation("查看设备上报的数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        List<Map<String, Object>> list = deviceService.queryServiceProperties(iotId);

        return success(list);
    }

    /**
     * 查询产品详情(报警规则模块使用)
     *
     * @param productKey
     * @return
     */
    @ApiOperation("查询产品详情(报警规则模块使用)")
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProductAlertRule(@PathVariable String productKey) {

        return deviceService.queryProductAlertRule(productKey);
    }
}
